package cn.shujuhai.common.controller.department;

import cn.shujuhai.common.aop.SysLog;
import cn.shujuhai.common.domain.Department;
import cn.shujuhai.common.pluging.data.ResultVo;
import cn.shujuhai.common.service.DepartmentService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Author: NiuYaHu
 * @Date: 2025/6/12 09:42
 * @Description:
 **/
@RestController
@RequiredArgsConstructor
@RequestMapping("/department")
public class DepartmentController {
    final DepartmentService departmentService;

    /**
     * 部门的模糊查询
     *
     * @param departmentName 部门名称
     * @return 返回父级部门集合
     */
    @SysLog(value = "部门的模糊查询", operationType = "查询", module = "部门模块")
    @GetMapping("/getParent")
    public ResultVo<?> getParent(@Validated String departmentName) {
        return this.departmentService.getDepartmentsWithParentsByNameLike(departmentName);
    }

    /**
     * 根据部门ID删除部门信息
     *
     * @param departmentId 部门id
     * @return 删除成功或失败的结果及响应信息
     */
    @SysLog(value = "删除部门", operationType = "编辑", module = "部门模块")
    @PostMapping("/delete/{departmentId}")
    public ResultVo<Department> delete(@PathVariable Integer departmentId) {
        return this.departmentService.removeId(departmentId);
    }

    /**
     * 部门新增
     *
     * @param department 部门对象
     * @return 新增部门对象
     */
    @SysLog(value = "新增部门", operationType = "新增", module = "部门模块")
    @PostMapping("/add")
    public ResultVo<?> save(@Validated @RequestBody Department department) {
        return this.departmentService.saveDepartment(department);

    }

    /**
     * 根据部门Id获取部门列表
     *
     * @param departmentId 部门Id
     * @return 部门集合
     */
    @SysLog(value = "根据部门Id获取部门列表", operationType = "查询", module = "部门模块")
    @GetMapping("/getDepartmentByDepartmentId/{departmentId}")
    public ResultVo<?> getDepartmentByDepartmentId(@PathVariable Long departmentId) {
        return this.departmentService.getDepartmentByDepartmentId(departmentId);
    }

    /**
     * 部门编辑
     *
     * @param department 部门对象
     * @return 获取修改部门id, 返回更新后的部门
     */
    @SysLog(value = "部门编辑", operationType = "编辑", module = "部门模块")
    @PostMapping("/update")
    public ResultVo<Department> update(@Validated @RequestBody Department department) {
        return this.departmentService.updateDepartment(department);
    }

    /**
     * 获取部门名称
     *
     * @return 部门名称集合
     */
    @SysLog(value = "获取部门列表", operationType = "查询", module = "部门模块")
    @GetMapping("/list")
    public ResultVo<?> getDepartmentList() {
        return ResultVo.success(this.departmentService.list());
    }

    /**
     * 根据登录用户获取部门
     *
     * @return 部门
     */
    @SysLog(value = "获取登录用户部门", operationType = "查询", module = "部门模块")
    @GetMapping("/departmentByUserId")
    public ResultVo<?> getDepartmentByUserId() {
        return this.departmentService.getDepartmentByUserId();
    }

    /**
     * 根据用户Id查询部门Id
     *
     * @param userId 用户Id
     * @return 返回部门列表
     */
    @SysLog(value = "根据用户Id查询部门Id", operationType = "查询", module = "部门模块")
    @GetMapping("/getDepartmentIdsByUserId")
    public ResultVo<?> departmentsByUserId(@RequestParam Long userId) {
        return this.departmentService.departmentIdsByUserId(userId);
    }

    /**
     * 将部门Id存储到session中
     *
     * @param request 请求体
     * @return 返回成功或失败的响应信息
     */
    @PostMapping("/session/setDepartmentId")
    public ResultVo<?> setDepartmentId(@RequestBody Map<String, Long> request, HttpServletRequest httpRequest) {
        return this.departmentService.sessionDepartmentId(request, httpRequest);
    }

    @GetMapping("/session/getDepartmentId")
    public ResultVo<?> getDepartmentId(HttpServletRequest request) {
        return this.departmentService.getSessionDepartmentId(request);
    }

}
